package net.amsoft.iservice.management.webapp.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.amsoft.iservice.management.util.IBrokerConstants;
import net.amsoft.iservice.management.util.IBrokerUtil;
import net.amsoft.iservice.management.webapp.struts.form.SaveToDBForm;
import net.amsoft.iservice.util.IServiceConstants;
import net.amsoft.iservice.util.IServiceUtil;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Gets called when user wants to save or cancel the modified ad-banner URL 
 * or static links <br>
 * 
 * If user has opted to cancel, the user is simply redirected to the IServices 
 * Manager Application Homepage <br>
 * 
 * If user has opted to save the changes, old data is replaced by the new 
 * data in the specified iservice database <br>
 * 
 * @author mukta
 */
public class SaveToDBAction extends Action 
{
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception 
	{
		SaveToDBForm myForm = (SaveToDBForm) form;
		String dataType = myForm.getDataType();
		
		// Check if user has come through proper channel or simply typed URL in browser
	    if (dataType==null)
	    {
            IServiceUtil.log(IServiceConstants.DEBUG, this, "execute(): redirect to=" + 
            		IBrokerConstants.FWD_LOGIN);
	        return mapping.findForward(IBrokerConstants.FWD_LOGIN);
	    }       

		if ((isCancelled(request)) || ("cancel".equals(myForm.getAction())))
		{
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Cancelled to save changed " + dataType + "..");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): redirect to=" + IBrokerConstants.FWD_CANCEL);
			return mapping.findForward(IBrokerConstants.FWD_CANCEL);
		}
		
		// Check if ad-banner or static links
		if ("ad-banner".equals(dataType))
		{
			// Save ad-banner link to database
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Saving ad-banner info to database");
			String banner = (String) request.getSession().getAttribute("BannerLink");
			String serverName = (String) request.getSession().getAttribute("ServerName");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Calling \"IBrokerUtil.processAdBanner()\" to update database");
			IBrokerUtil.processAdBanner(serverName, banner, (String) request.getSession().getAttribute("Service"));
		    
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Setting session attribute Banner=" + banner);
            request.getSession().setAttribute("Banner", banner);
		}
		else
		{
			// Code to save static links to database
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Saving static links info to database");
			String linksData = (String) request.getSession().getAttribute("LinksData");
			String serverName = (String) request.getSession().getAttribute("ServerName");
            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Calling \"IBrokerUtil.processLinks()\" to update database");
			IBrokerUtil.processLinks(serverName, linksData, (String) request.getSession().getAttribute("Service"));

            IServiceUtil.log(IServiceConstants.DEBUG, this, 
                    "execute(): Setting session attribute Links=" + 
                    (String) request.getSession().getAttribute("LinksData"));
			request.getSession().setAttribute("Links", (String) request.getSession().getAttribute("LinksData"));
		}

        IServiceUtil.log(IServiceConstants.DEBUG, this, 
                "execute(): redirect to="+ IBrokerConstants.FWD_SUCCESS);
		return mapping.findForward(IBrokerConstants.FWD_SUCCESS);
	}
	
}